﻿using DispatcherProhect.CustomJob;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DispatcherProhect
{
    /// <summary>
    /// 1.Nuget添加
    /// 三大核心对象
    /// IScheduler :单元/实例，在这里去完成定时任务的配置 只有单元启动 里面的作业才能正常运行
    /// Ijob 任务 定时执行的动作就是job
    /// ITrigger 定时策略
     /// </summary>
    public class DispatcherManager
    {
        ///三大核心对象
        ///
        public static async Task Init()
        {
            #region
            StdSchedulerFactory factory = new StdSchedulerFactory();
            IScheduler scheduler = await factory.GetScheduler();
            await scheduler.Start();
            #endregion

            {
                //准备任务第二
                //创建作业
              IJobDetail jobDetail=  JobBuilder.Create<TestJob>().WithIdentity("testjob", "group1").WithDescription("this is test job").Build();
                //准备定时策略
                ITrigger trigger = TriggerBuilder.Create().WithIdentity("testtrigger1", "group1")
                    .StartAt(new DateTimeOffset(DateTime.Now.AddSeconds(10)))
                    //.StartNow() //startat
                    .WithCronSchedule("0 0/1 * * *?")
                    .WithDescription("this is Triggger job")
                    .Build();

                await scheduler.ScheduleJob(jobDetail, trigger);
                Console.WriteLine("任务添加成功");
            }
        }
    }
}
